<!--
 * @Author: early-autumn
 * @Date: 2020-05-20 17:56:34
 * @LastEditors: early-autumn
 * @LastEditTime: 2020-06-17 15:38:25
-->
<template>
  <div>
    <el-form ref="form" :model="form" size="small" :rules="rules" inline>
      <el-form-item prop="keyword">
        <el-input v-model="form.keyword" placeholder="姓名/手机号">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="handleSearchPatientVisible"
            >搜索</el-button
          >
        </el-input>
      </el-form-item>
    </el-form>

    <el-form v-if="patient" inline>
      <el-row>
        <el-col :span="6">
          <el-form-item label="姓名：">{{ patient.name }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="年龄："> {{ patient.age }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="性别：">{{
            formatterDict(patient.sex, 'sexOptions')
          }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="手机号：">
            {{ patient.phoneNumber }}</el-form-item
          >
        </el-col>
      </el-row>
    </el-form>

    <search-patient
      v-model="searchPatientVisible"
      :keyword="form.keyword"
      @ok="handlePatient"
    />

    <create-temporary-patient
      v-model="createTemporaryPatientVisible"
      @ok="handlePatient"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import SearchPatient from './SearchPatient'
import CreateTemporaryPatient from './CreateTemporaryPatient'

export default {
  name: 'UserMatcher',
  components: {
    SearchPatient,
    CreateTemporaryPatient,
  },
  data() {
    return {
      form: {
        keyword: undefined,
      },
      rules: {
        keyword: {
          required: true,
          message: '姓名/手机号不能为空',
          trigger: 'blur',
        },
      },
      // 显示搜索患者
      searchPatientVisible: false,
      // 显示新建临时患者
      createTemporaryPatientVisible: false,
      // 当前患者
      patient: undefined,
    }
  },
  computed: {
    ...mapGetters('dict', ['sexOptions']),
  },
  methods: {
    handleSearchPatientVisible() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.searchPatientVisible = true
        }
      })
    },
    // handleCreateTemporaryPatientVisible() {
    //   this.createTemporaryPatientVisible = true
    // },
    handlePatient(e) {
      this.patient = e
      this.searchPatientVisible = false
      this.$emit('change', e)
    },
  },
}
</script>

<style lang="scss" scoped></style>
